package com.dex4d.ui.qr.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.dex4d.BuildConfig;
import com.dex4d.R;
import com.dex4d.dialog.LoadingManager;
import com.dex4d.ui.qr.EasyDoneActivity;
import com.dex4d.ui.qr.model.PayData;
import com.dex4d.ui.qr.model.QRPay;
import com.dex4d.ui.qr.model.QRResult;
import com.dex4d.ui.qr.utils.AddressUtils;
import com.dex4d.ui.qr.utils.TipUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class EasyPayAuthFragment extends Fragment implements QRPay.OnQRPayListener {
    private static final String ARG_PARAM1 = "param1";

    private String mParamUrl;

    @BindView(R.id.txt_auth_info)
    TextView payInfo;

    @BindView(R.id.txt_auth_title)
    TextView payAmount;

    @BindView(R.id.txt_show_address)
    TextView payFrom;

    @BindView(R.id.txt_show_time)
    TextView payTo;

    @BindView(R.id.txt_show_fee)
    TextView payFee;

    @BindView(R.id.btn_confirm)
    Button btn_confirm;

    @BindView(R.id.btn_reject)
    Button btn_reject;

    QRPay qrPay;
    LoadingManager loadingManager;

    public EasyPayAuthFragment() {
        // Required empty public constructor
    }


    public static EasyPayAuthFragment newInstance(String param1) {
        EasyPayAuthFragment fragment = new EasyPayAuthFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParamUrl = BuildConfig.QR_URL_API + getArguments().getString(ARG_PARAM1);
        }

        qrPay = new QRPay(getActivity(), this);
        loadingManager= new LoadingManager(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_easy_pay_auth, container, false);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        qrPay.payInfo(mParamUrl);
    }

    @OnClick(R.id.btn_confirm)
    public void confirmClick() {
        if (qrPay.getPayData() == null) return;

        loadingManager.showLoadingDialog();
        qrPay.payConfirm(mParamUrl);
    }

    @OnClick(R.id.btn_reject)
    public void rejectClick() {
        loadingManager.showLoadingDialog();
        qrPay.payReject(mParamUrl);
    }

    /**
     * 获取支付信息
     *
     * @param result
     */
    @Override
    public void payInfo(QRResult result) {
        if (result.paydata != null) {
            if (result.isOk()) {
                showUI(result);
            } else {
                TipUtils.showTips(getActivity(), result);
            }

        }
    }

    /**
     * 支付成功
     *
     * @param result
     */
    @Override
    public void success(QRResult result) {
        loadingManager.dismissLoadingDialog();
        qrPay.setTxHash(result.txhash);
        EasyDoneActivity.start(getActivity(), mParamUrl, qrPay.getPayData());
        getActivity().finish();
    }

    /**
     * 支付错误
     *
     * @param result
     */
    @Override
    public void error(QRResult result) {
        loadingManager.dismissLoadingDialog();
        TipUtils.showTips(getActivity(), result);
    }

    /**
     * 拒绝签名
     *
     * @param result
     */
    @Override
    public void reject(QRResult result) {
        loadingManager.dismissLoadingDialog();
        getActivity().finish();
    }

    /**
     * UI展示
     *
     * @param result
     */
    private void showUI(QRResult result) {
        PayData payData = result.paydata;

        payAmount.setText(payData.getAmount(getActivity()));

        String  info = payData.getDesc(getActivity());
        payInfo.setText(info);
        payFrom.setText(AddressUtils.convertAddress(payData.from));
        payTo.setText(AddressUtils.convertAddress(payData.to));
        payFee.setText(payData.txFee);

    }

}
